using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SpoonCore.Storage
{
	public abstract class AbstractStorageItem
	{
		private SpoonIdentifier _id;
		private DateTime _changeTime;

		internal AbstractStorageItem()
		{
		}

		public AbstractStorageItem(SpoonIdentifier id)
		{
			_id = id;
		}

		public SpoonIdentifier Id
		{
			get { return _id; }
		}

		public DateTime ChangeTime
		{
			get { return _changeTime; }
			set { _changeTime = value; }
		}

		internal byte[] GetBaseBytes()
		{
			MemoryStream stream = new MemoryStream();
			BinaryWriter bw = new BinaryWriter(stream);
			bw.Write(Id.GetBytes());
			bw.Write(ChangeTime.Ticks);
			return stream.ToArray();
		}

		internal void SetBaseBytes(byte[] data)
		{
			BinaryReader br = new BinaryReader(new MemoryStream(data));
			_id = new SpoonIdentifier(br);
			ChangeTime = new DateTime(br.ReadInt64());
		}

		public abstract byte[] GetBytes();
		public abstract void SetBytes(byte[] data);
	}
}
